package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.dao.DividendDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.Dividend;
import org.springframework.stereotype.Repository;

@Repository
public class DividendDaoImpl extends AbstractDaoImpl<Dividend, Integer>
		implements DividendDao {

	protected DividendDaoImpl() {
		super(Dividend.class);
	}

	@Override
	public void saveDividend(Dividend dividend) {
		saveOrUpdate(dividend);
	}

	@Override
	public List<Dividend> findDividend(String companyName) {
		return findByCriteria(Restrictions.like("name", companyName,
				MatchMode.START));
	}

	@Override
	public List<Dividend> searchDividend(int companyId, Date fromDate,
			Date toDate) {
		Criteria criteria = getCurrentSession().createCriteria(Dividend.class);
		if (companyId > 0) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}
		return findByCriteria(criteria);
	}

	@Override
	public Page<Dividend> searchDividend(int companyId, Date fromDate,
			Date toDate, GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(Dividend.class);
		if (companyId > 0) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}

		return findByCriteria(criteria, gridPramDTO);
	}

	@Override
	public List<Dividend> searchDividend(int companyId) {
		return findByCriteria(Restrictions.eq("company.id", companyId));
	}

}
